/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import Configuration.Configuration;
import Regist.regist;
import user.user;
import PaperManagement.Infor;
import java.awt.event.KeyEvent;
import static java.awt.image.ImageObserver.WIDTH;
import java.net.ConnectException;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author dianer
 */
public class Login extends javax.swing.JFrame {

    /**
     * Creates new form UI
     */
    private Infor infor;

    public Login(Infor tmp) {
        infor = tmp;
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new javax.swing.JLabel();
        userid = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        useridtext = new javax.swing.JTextField();
        login = new javax.swing.JButton();
        regist = new javax.swing.JButton();
        configuration = new javax.swing.JButton();
        passwordtext = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setResizable(false);

        title.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        title.setText("PAPER MANAGEMENT SYSTEM ");

        userid.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        userid.setText("UserId");

        password.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        password.setText("PassWord");

        useridtext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useridtextActionPerformed(evt);
            }
        });
        useridtext.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                useridtextKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                useridtextKeyTyped(evt);
            }
        });

        login.setText("Login");
        login.setPreferredSize(new java.awt.Dimension(63, 28));
        login.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loginMouseClicked(evt);
            }
        });
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });

        regist.setText("Regist");
        regist.setPreferredSize(new java.awt.Dimension(69, 28));
        regist.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                registMouseClicked(evt);
            }
        });
        regist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registActionPerformed(evt);
            }
        });

        configuration.setIcon(new javax.swing.ImageIcon(getClass().getResource("/login/configuration.jpg"))); // NOI18N
        configuration.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        configuration.setPreferredSize(new java.awt.Dimension(35, 35));
        configuration.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                configurationMouseClicked(evt);
            }
        });
        configuration.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configurationActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userid)
                    .addComponent(password))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(useridtext, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                    .addComponent(passwordtext))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(regist, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(configuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(58, Short.MAX_VALUE)
                        .addComponent(title)))
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(title)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(userid)
                            .addComponent(useridtext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password)
                            .addComponent(passwordtext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(regist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(configuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
        // TODO add your handling code here:
        boolean flag = false;
        Vector columns = new Vector();
        columns.add("primarykey");
        columns.add("psd");
        Vector column = new Vector();
        column.add("primarykey");

        try {
            Vector[] vec = infor.jx.getTableRows("User", columns, "*");
            for (int j = 0; j < vec[0].size(); j++) {
                if (vec[0].get(j).toString().equals(useridtext.getText()) && vec[1].get(j).toString().equals(passwordtext.getText())) {
                    flag = true;
                    //System.out.print("aaaaaaaaaaaa");
                }
            }

            if (flag) {
                infor.currentUser = infor.jx.getTableRows("User", column, useridtext.getText())[0].get(0).toString();
                Vector type = new Vector();
                type.add("type");
                String sType = infor.jx.getTableRows("User", type, infor.currentUser)[0].get(0).toString();
                if (sType.equals("common")) {
                    new user(infor).setVisible(true);
                } else if (sType.equals("reviewer")) {
                    new Reviewer.Reviewer(infor).setVisible(true);
                } else if (sType.equals("chairman")) {
                    new Reviewer.Reviewer(infor).setVisible(true);
                }
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null, "Please check your UserId & Password", "Notice", JOptionPane.OK_OPTION);
            }
        } catch (Exception e) {
            System.out.println("Network Error");
        }
    }//GEN-LAST:event_loginActionPerformed

    private void registActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registActionPerformed
        new regist(infor).setVisible(true); // TODO add your handling code here:
    }//GEN-LAST:event_registActionPerformed

    private void configurationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configurationActionPerformed
        new Configuration(infor).setVisible(true);   // TODO add your handling code here:
    }//GEN-LAST:event_configurationActionPerformed

    private void loginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginMouseClicked
    }//GEN-LAST:event_loginMouseClicked

    private void registMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_registMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_registMouseClicked

    private void configurationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_configurationMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_configurationMouseClicked

    private void useridtextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_useridtextKeyTyped
        if ((evt.getKeyChar() >= KeyEvent.VK_0 && evt.getKeyChar() <= evt.VK_9)
                || evt.getKeyChar() == evt.VK_ENTER || evt.getKeyChar() == evt.VK_TAB
                || evt.getKeyChar() == evt.VK_BACK_SPACE || evt.getKeyChar() == evt.VK_DELETE
                || evt.getKeyChar() == evt.VK_LEFT || evt.getKeyChar() == evt.VK_RIGHT
                || evt.getKeyChar() == evt.VK_ESCAPE) {
            return;
        }
        evt.consume();

        //if(evt.getKeyChar()<='0'||evt.getKeyChar()>='9') 
        //{ evt.setKeyCode(0);System.out.print("bbbbbbbbbbb"); }// TODO add your handling code here:

    }//GEN-LAST:event_useridtextKeyTyped

    private void useridtextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_useridtextKeyPressed
        //if(evt.getKeyChar()<='0'||evt.getKeyChar()>='9') 
        // { System.out.print(evt.getKeyCode()); }   // TODO add your handling code here:
    }//GEN-LAST:event_useridtextKeyPressed

    private void useridtextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useridtextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_useridtextActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton configuration;
    private javax.swing.JButton login;
    private javax.swing.JLabel password;
    private javax.swing.JPasswordField passwordtext;
    private javax.swing.JButton regist;
    private javax.swing.JLabel title;
    private javax.swing.JLabel userid;
    private javax.swing.JTextField useridtext;
    // End of variables declaration//GEN-END:variables
}
